const Editor = require('../../../src/index');
const Command = require('../../../src/command/');
const expect = require('chai').expect;
const data = require('../../../demos/data/sample.json');
const Util = Editor.Util;

// function expectToDeepAlmost(truely, expected, error = 1) {
//   Util.each(truely, (tv, tk) => {
//     const ev = expected[tk];
//     const bool = (tv > ev - error && tv < ev + error);
//     expect(bool).equal(true);
//   });
// }

describe('command', () => {
  const editor = new Editor();
  const dom = Util.createDOM(`
    <div>
      <div id="page"></div>
    </div>
  `);
  document.body.appendChild(dom);
  const page = new Editor.Flow({
    graph: {
      container: 'page',
      height: 300,
      width: 300
    }
  });
  page.read(data);
  page.add('node', {
    x: 200,
    y: 150,
    id: 'haha',
    label: '哈哈哈哈哈哈'
  });
  page.add('node', {
    x: 200,
    y: 150,
    id: 'hehe',
    size: [ 50, 50 ],
    label: '呵呵'
  });
  editor.add(page);
  // page.setSelected('9cf739d1', true);
  // Command.execute('delete', editor);
  // Command.execute('clear', editor);
  // Command.execute('undo', editor);
  // Command.execute('undo', editor);
  // Command.execute('redo', editor);
  // Command.execute('redo', editor);
  // Command.execute('undo', editor);
  // Command.execute('undo', editor);
  // page.setSelected([ 'haha', 'hehe' ], true);
  // Command.execute('delete', editor);
  // Command.execute('clear', editor);
  // Command.execute('undo', editor);
  // Command.execute('redo', editor);
  // Command.execute('undo', editor);
  // Command.execute('undo', editor);
  // Command.execute('undo', editor);
  it('undo', () => {
    expect(Command.enable('undo', editor)).equal(false);
    Command.execute('clear', editor);
    expect(Command.enable('undo', editor)).equal(true);
    Command.execute('undo', editor);
  });
  it('redo', () => {
    expect(Command.enable('redo', editor)).equal(true);
    Command.execute('redo', editor);
    expect(Command.enable('redo', editor)).equal(false);
    Command.execute('undo', editor);
  });
  it('clear', () => {
    const itemLength = page.getItems().length;
    expect(Command.enable('clear', editor)).equal(true);
    const command = Command.execute('clear', editor);
    expect(page.getItems().length).equal(0);
    command.back(editor);
    expect(page.getItems().length).equal(itemLength);
  });
  it('delete', () => {
    const itemLength = page.getItems().length;
    expect(Command.enable('delete', editor)).equal(false);
    page.setSelected('9cf739d1', true);
    expect(Command.enable('delete', editor)).equal(true);
    const command = Command.execute('delete', editor);
    expect(page.find('9cf739d1')).equal(undefined);
    command.back(editor);
    expect(page.getItems().length).equal(itemLength);
  });
  it('toBack', () => {
    page.clearSelected();
    expect(Command.enable('toBack', editor)).equal(false);
    page.setSelected([ 'haha', 'hehe' ], true);
    const command = Command.execute('toBack', editor);
    expect(Util.getIndex(page.find('haha').getGraphicGroup())).equal(0);
    expect(Util.getIndex(page.find('hehe').getGraphicGroup())).equal(1);
    command.back(editor);
    expect(Util.getIndex(page.find('haha').getGraphicGroup())).equal(10);
    expect(Util.getIndex(page.find('hehe').getGraphicGroup())).equal(11);
  });
  it('toFront', () => {
    page.clearSelected();
    expect(Command.enable('toFront', editor)).equal(false);
    page.setSelected([ 'haha', 'hehe' ], true);
    const command = Command.execute('toFront', editor);
    expect(Util.getIndex(page.find('haha').getGraphicGroup())).equal(10);
    expect(Util.getIndex(page.find('hehe').getGraphicGroup())).equal(11);
    command.back(editor);
    expect(Util.getIndex(page.find('haha').getGraphicGroup())).equal(10);
    expect(Util.getIndex(page.find('hehe').getGraphicGroup())).equal(11);
  });
  it('addGroup', () => {
    page.clearSelected();
    expect(Command.enable('addGroup', editor)).equal(false);
    page.setSelected([ 'haha', 'hehe' ], true);
    const command = Command.execute('addGroup', editor);
    expect(page.find('haha').getParent()).not.equal(undefined);
    command.back(editor);
    expect(page.find('haha').getParent()).equal(undefined);
  });
  it('unGroup', () => {
    expect(Command.enable('unGroup', editor)).equal(false);
    page.setSelected([ 'haha', 'hehe' ], true);
    page.addGroup();
    page.clearSelected();
    page.setSelected(page.find('haha').getParent(), true);
    expect(Command.enable('unGroup', editor)).equal(true);
    const command = Command.execute('unGroup', editor);
    expect(page.find('haha').getParent()).equal(undefined);
    command.back(editor);
    expect(page.find('haha').getParent()).not.equal(undefined);
  });
  it('copy && paste', () => {
    page.setSelected([ '9cf739d1', 'haha', 'hehe' ], true);
    Command.execute('copy', editor);
    Command.execute('paste', editor);
  });
  it('multiSelect', () => {
    expect(Command.enable('multiSelect', editor)).equal(true);
    const command = Command.execute('multiSelect', editor);
    expect(Command.enable('multiSelect', editor)).equal(false);
    command.back(editor);
    expect(page.getMode()).equal('default');
  });
  it('move', () => {
    expect(Command.enable('move', editor)).equal(true);
    const command = Command.execute('move', editor);
    expect(Command.enable('move', editor)).equal(false);
    command.back(editor);
    expect(page.getMode()).equal('default');
  });
  it('zoomIn', () => {
    const zoom = page.getZoom();
    expect(Command.enable('zoomIn', editor)).equal(true);
    page.zoom(20);
    expect(Command.enable('zoomIn', editor)).equal(false);
    page.zoom(1);
    const command = Command.execute('zoomIn', editor);
    expect(page.getZoom()).equal(1.1);
    command.back(editor);
    expect(page.getZoom()).equal(zoom);
  });
  it('zoomOut', () => {
    const zoom = page.getZoom();
    expect(Command.enable('zoomOut', editor)).equal(true);
    page.zoom(0.02);
    expect(Command.enable('zoomOut', editor)).equal(false);
    page.zoom(1);
    const command = Command.execute('zoomOut', editor);
    expect(page.getZoom()).equal(0.9);
    command.back(editor);
    expect(page.getZoom()).equal(zoom);
  });
  it('autoZoom', () => {
    // const matrix = page.getMatrix();
    // expect(Command.enable('autoZoom', editor)).equal(true);
    // const command = Command.execute('autoZoom', editor);
    // expectToDeepAlmost(page.getMatrix(), [ 0.5883629671621454, 0, 0, 0, 0.5883629671621454, 0, -5.122170009670214, 10.821772081561988, 1 ]);
    // command.back(editor);
    // expect(page.getMatrix()).to.deep.equal(matrix);
  });
  it('resetZoom', () => {
    // expect(Command.enable('resetZoom', editor)).equal(true);
    // page.zoom({
    //   x: 10,
    //   y: 10,
    // }, 0.3);
    // const matrix = Util.clone(page.getMatrix());
    // const command = Command.execute('resetZoom', editor);
    // expectToDeepAlmost(page.getMatrix(), [ 1, 0, 0, 0, 1, 0, -140, -140, 1 ]);
    // command.back(editor);
    // expect(page.getMatrix()).to.deep.equal(matrix);
  });
  it('destroy', () => {
    editor.destroy();
  });
});
